﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> fillPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    Document pdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Load the PDF document with a form to fill</span>
        <span style='color:#800000; font-weight:bold; '>string</span> pdfFileWithForm <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/PDF_Files/Fill_PDF_Forms.pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        pdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span>pdfFileWithForm<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
        <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
        pdfDocument<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the First Name text box field by name from PDF form fields collection and fill a text value</span>
        PdfFormField firstNameTextBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>firstName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>firstNameTextBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            firstNameTextBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> firstNameTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the Last Name text box field by name from PDF form fields collection and fill a text value</span>
        PdfFormField lastNameTextBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>lastName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>lastNameTextBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            lastNameTextBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> lastNameTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the Password text box field by name from PDF form fields collection and fill a text value</span>
        PdfFormField passwordTextBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>password</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>passwordTextBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            passwordTextBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> passwordTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the Gender radio buttons group field by name from form fields collection and set the selected value</span>
        PdfFormField genderRadioGroupField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>gender</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>genderRadioGroupField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            genderRadioGroupField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> maleRadioButton<span style='color:#808030; '>.</span>Checked ? <span style='color:#800000; '>"</span><span style='color:#0000e6; '>male</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>:</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>female</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the 'I have a car' check box field by name from form fields collection and set the checked status</span>
        PdfFormField haveCarCheckBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>haveCar</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>haveCarCheckBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            haveCarCheckBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> haveCarCheckBox<span style='color:#808030; '>.</span>Checked<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the 'Vehicle Type' combo box field by name from form fields collection and set the selected value</span>
        PdfFormField vehicleTypeComboBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>vehicleType</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>vehicleTypeComboBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            vehicleTypeComboBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> carTypeComboBox<span style='color:#808030; '>.</span>SelectedValue<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Get the Comments multi line text box field by name from PDF form fields collection and fill a text value</span>
        PdfFormField commentsTextBoxField <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>Fields<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>comments</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>commentsTextBoxField <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            commentsTextBoxField<span style='color:#808030; '>.</span>Value <span style='color:#808030; '>=</span> commentsTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Flatten PDF form fields if this was requested</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>flattenPdfFormCheckBox<span style='color:#808030; '>.</span>Checked<span style='color:#808030; '>)</span>
            pdfDocument<span style='color:#808030; '>.</span>Form<span style='color:#808030; '>.</span>FlattenFields<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Save the PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Set response content type</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=Filled_PDF_Form.pdf.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
        Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
        Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>finally</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Close the document to stamp</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>pdfDocument <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#808030; '>)</span>
            pdfDocument<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</span>